package com.example.myapplication.activity;

import android.app.ProgressDialog;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import com.example.myapplication.R;
import com.example.myapplication.activity.RegisterActivity;
import com.example.myapplication.activity.ShopActivity;
import com.example.myapplication.utils.DBHelper;

public class LoginActivity extends AppCompatActivity {
    private SQLiteDatabase db;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        // 设置隐藏状态栏
//        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);
        getSupportActionBar().hide();//隐藏标题栏
        EditText accountEdit = findViewById(R.id.editText1);
        EditText passwordEdit = findViewById(R.id.editText2);

        boolean registrationSuccess = getIntent().getBooleanExtra("registrationSuccess", false);
        if (registrationSuccess) {
            // 显示注册成功提示
            Toast.makeText(LoginActivity.this, "注册成功，快去登录吧！", Toast.LENGTH_SHORT).show();
        }

        Button loginButton = findViewById(R.id.button1);
        loginButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String account = accountEdit.getText().toString();
                String password = passwordEdit.getText().toString();

                ProgressDialog progressDialog = new ProgressDialog(LoginActivity.this);
                progressDialog.setTitle("登录");
                progressDialog.setMessage("加载中...");
                progressDialog.setCancelable(true);
                progressDialog.show();

                // 使用 SQLite 数据库进行账号密码验证
                if (checkUser(account, password)) {
                    Intent intent = new Intent(LoginActivity.this, ShopActivity.class);
                    startActivity(intent);
                    finish();
                } else {
                    Toast.makeText(LoginActivity.this, "账号或密码错误", Toast.LENGTH_SHORT).show();
                }

                progressDialog.cancel();
            }

        });

        Button registerButton = findViewById(R.id.button2);
        registerButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // 创建 Intent 对象，指定目标 Activity 的类
                Intent intent = new Intent(LoginActivity.this, RegisterActivity.class);
                // 启动目标 Activity
                startActivity(intent);
            }
        });

        // 初始化 SQLite 数据库
        DBHelper dbHelper = new DBHelper(this);
        db = dbHelper.getWritableDatabase();
    }

    private boolean checkUser(String account, String password) {
        Cursor cursor = db.rawQuery("SELECT * FROM user WHERE account=? AND password=?", new String[]{account, password});
        int count = cursor.getCount();
        cursor.close();
        return count > 0;
    }
}
